<?php

namespace backend\modules\goods\models;

use Yii;

/**
 * This is the model class for table "attribute".
 *
 * @property string $attr_id
 * @property string $attr_name
 * @property string $attr_cat_id
 * @property string $attrValue
 */
class Attribute extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'attribute';
    }

    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
//            TimestampBehavior::className(),
            // 名为myBehavior4的行为，也是给出了MyBehavior类的配置数组
            'uuid' => [
                'class' => \common\behavior\uuidBehavior::className(),
                'owner' => $this
            ]
        ];
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            ['attr_id','default','value'=>$this->getUUID()],
            [['attr_id'], 'required'],
            [['attr_id', 'attr_cat_id'], 'string', 'max' => 40],
            [['attr_name'], 'string', 'max' => 60],
            [['attrValue'],'string'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'attr_id' => Yii::t('app', 'Attr ID'),
            'attr_name' => Yii::t('app', '属性名称'),
            'attr_cat_id' => Yii::t('app', '属性分类'),
            'attrValue' => Yii::t('app', '属性值'),
        ];
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getAttrcat()
    {
        return $this->hasOne(Attrcat::className(), ['attr_cat_id' => 'attr_cat_id']);
    }

    /**
     * 下拉数据数组
     *
     * attrcatAlias('all');
     * @param $type
     * @return array|\backend\modules\goods\models\Attrcat[]
     */
    public static function attrcatAlias($type='all')
    {
        $allFilter = array();
        if($type=="all"){
            $Attrcat =  Attrcat::find()->select(['attr_cat_id','attr_cat_name'])->asArray()->all();
        }
        foreach($Attrcat as $k=>$v) {
            $allFilter[$v['attr_cat_id']] = $v['attr_cat_name'];
        }
        return $allFilter;
    }
}
